﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using TFXK.Model;
using TFXK.BLL;
using TFXK.Common;
using log4net;

public partial class admin_CaseManage_Action : BasePage
{
    private static readonly ILog log = LogManager.GetLogger(typeof(admin_CaseManage_Action));
    private static readonly NewsBLL bll = new NewsBLL();
    private static readonly CategoryBLL bllCategory = new CategoryBLL();
    private static readonly PicturesBLL bllPictures = new PicturesBLL();
    public static string codeNo = "jdal";
    public int i = 0;

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                InitAction();
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }

    }
    #endregion

    #region 绑定动作
    private void InitAction()
    {
        if (Request.QueryString["codeNo"] != null)
        {
            codeNo = Request.QueryString["codeNo"] + "";
        }
        if (Request.QueryString["type"] != null)
        {
            BindSelType();
            string type = Request.QueryString["type"].ToString();
            string title = string.Empty;// 设置标题
            // 判断动作
            switch (type)
            {
                case "add":
                    title = "案例添加";
                    txtDate.Text = DateTime.Now.ToString("yyyy/MM/dd");
                    break;
                case "modify":
                    if (Request.QueryString["id"] != null)
                    {
                        title = "案例修改";
                        int id = int.Parse(Request.QueryString["id"].ToString());
                        InitData(id);
                    }
                    else
                    {
                        Response.Redirect("Default.aspx", false);
                    }
                    break;

            }
            this.Title = title;
            this.hdfAction.Value = type;
        }
        else
        {
            Response.Redirect("Default.aspx", false);
        }
    }
    #endregion

    #region 绑定
    private void InitData(int id)
    {
        News model = bll.GetModel(id);
        if (model != null)
        {
            txtTitle.Text = model.title;
            this.txtDate.Value = model.createTime;
           // this.txtClick.Text = model.clicks + "";
            this.txtOrderby.Text = model.orderId + "";
            txtContent.Value = model.description;
           // this.txtSource.Text = model.source;
          //  ddlShowType.SelectedIndex = int.Parse(model.isSlideOn);
            ddlState.SelectedValue = model.state + "";
            txtOrderby.Text = model.orderId + "";
            //分类编号 
            ddlType.SelectedValue = model.parentId + "";
            BindImages(model.id, model.parentId);
        }
    }
    #endregion

    #region 添加/修改
    protected void ibtnAdd_Click(object sender, EventArgs e)
    {
        //验证代码
        try
        {
            TFXK.Model.News model = new TFXK.Model.News();
            try
            {
                model.id = int.Parse(Request.QueryString["id"].ToString());
            }
            catch { model.id = 0; }
            model.title = txtTitle.Text;
           // model.source = this.txtSource.Text;
            model.source = "";
            model.orderId = int.Parse(this.txtOrderby.Text.Equals("") ? "0" : this.txtOrderby.Text);
            model.parentId = int.Parse(this.ddlType.SelectedValue + "");
            model.isOutlLink = "0";
            model.createTime = this.txtDate.Text;
           // model.clicks = int.Parse(this.txtClick.Text.Equals("") ? "0" : this.txtClick.Text);
            model.clicks = 0;
            model.description = this.txtContent.Value;
            model.publisher = "";
            model.isSlideOn = "0";
            model.state = this.ddlState.SelectedValue;

            // 防止网页重复提交
            if (this.CheckRefreshed1.isRefreshed == false)
            {
                string type = this.hdfAction.Value.Trim();
                // 判断动作
                switch (type)
                {
                    case "add":
                        Add(model);
                        break;
                    case "modify":
                        Modify(model);
                        break;
                }
                UpLoadImages();
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
            Msg.Show("网络错误!原因:" + ex.Message);
        }
    }
    #endregion

    #region 添加
    private void Add(News model)
    {
        int rows = bll.Add(model);
        hdfPid.Value = rows + "";
        hdfTid.Value = model.parentId + "";
        if (rows > 0)
        {
            Msg.ShowAndRedirect("添加信息成功!", "Default.aspx?codeNo=" + codeNo);
        }
        else
        {
            Msg.Show("添加信息失败!");
        }
    }
    #endregion

    #region 修改
    private void Modify(News model)
    {
        bll.Update(model);
        hdfPid.Value = model.id + "";
        hdfTid.Value = model.parentId + "";
        Msg.ShowAndRedirect("修改信息成功!", "Default.aspx?codeNo=" + codeNo);
    }
    #endregion

    #region 返回
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?codeNo=" + codeNo, false);
    }
    #endregion

    #region 绑定下拉列表
    private void BindSelType()
    {
        DataSet ds = bllCategory.GetNextNodeByCode(codeNo);
        ddlType.DataSource = ds.Tables[0];
        ddlType.DataValueField = "id";
        ddlType.DataTextField = "title";
        ddlType.DataBind();
    }
    #endregion



    #region 图片上传
    private void UpLoadImages()
    {
        try
        {
            string saveDir = "../../uploads/"; //文件保存文件夹
            string serverFile = Server.MapPath(saveDir);
            int Nid = 0;
            foreach (DevExpress.Web.ASPxUploadControl.UploadedFile file in uploadControl.UploadedFiles)
            {
                Nid++;

                string imgExt = file.FileName.Substring(file.FileName.IndexOf('.'));
                string saveName = DateTime.Now.ToString("yyyyMMddhhmmss" + Nid) + imgExt;

                file.SaveAs(serverFile+saveName);
                TFXK.Model.Pictures pmodel = new TFXK.Model.Pictures();
                pmodel.imgPath = saveName;
                pmodel.imgAlt = file.FileName.Substring(0, file.FileName.IndexOf('.'));
                pmodel.parentID = int.Parse(hdfPid.Value);
                pmodel.typeID = int.Parse(hdfTid.Value);
                bllPictures.Add(pmodel);
            }
        }
        catch (Exception ex)
        {
            Msg.Show("网络错误:" + ex.Message);
        }
    }
    #endregion

    #region 图片加载
    private void BindImages(int pid,int tid)
    {
        try
        {
          rptImages.DataSource=  bllPictures.GetList(pid, tid);
          rptImages.DataBind();
        }
        catch (Exception ex)
        {
            Msg.Show("网络错误:" + ex.Message);
        }
    }
    #endregion
 
}
